﻿using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using Twins.Classes.Hanlder.HanlderStatement;
using Twins.Interface.IHandler;

namespace Twins.Classes.HanlderRegistry
{
    public static class CSharpStatementHanlderRegistry
    {
        private static readonly Dictionary<Type, Type> Handlers = InitHanlders();
         
        private static Dictionary<Type, Type> InitHanlders()
        {
            return new Dictionary<Type, Type>
            {
               {typeof (IExpressionStatement), typeof(HanlderForIExpressionStatement)},
               {typeof (IForStatement), typeof(HanlderForIForStatement)},
               {typeof (IIfStatement), typeof(HanlderForIIfStatement)},
               {typeof (IForeachStatement), typeof(HanlderForIForeachStatement)},
               {typeof (IWhileStatement), typeof(HanlderForIWhileStatement)},
               {typeof (IBlock), typeof(HanlderForIBlock)},
               {typeof (IDoStatement), typeof(HanlderForIDoStatement)},
               {typeof (ISwitchStatement), typeof(HandlerForISwitchStatement)}

            };
        }

        public static IHandler Get(object cSharpStatement) 
        {
            IEnumerable<IHandler> handlers = from type in Handlers.Keys
                                             where type.IsAssignableFrom(cSharpStatement.GetType())
                                             select Activator.CreateInstance(Handlers[type]) as IHandler;
            return handlers.FirstOrDefault();
         }
    }
}